﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DateTime starttime = this.dateTimePicker1.Value;
            DateTime endTime = this.dateTimePicker2.Value;
            if (endTime >= starttime)
            {
                for (int m = 0; m < 100; m++)
                {
                    var date1 = FirstDayOfMonth(starttime.AddMonths(m));
                    if (date1 < starttime)
                        date1 = starttime;
                    var date2 = LastDayOfMonth(date1);
                    if (date2 >= endTime)
                    {
                        date2 = endTime;
                        this.textBox1.Text += date1 + "到" + date2 + "\r\n";
                        break;
                    }
                    else
                    {
                        this.textBox1.Text += date1 + "到" + date2 + "\r\n";
                    }
                }
            }
        }
        /// <summary>
        /// 取得某月的第一天
        /// </summary>
        /// <param name="datetime">要取得月份第一天的时间</param>
        /// <returns></returns>
        private DateTime FirstDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day);
        }

        //// <summary>
        /// 取得某月的最后一天
        /// </summary>
        /// <param name="datetime">要取得月份最后一天的时间</param>
        /// <returns></returns>
        private DateTime LastDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day).AddMonths(1).AddDays(-1);
        }
    }
}
